package com.xckj.ibox.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.xckj.ibox.R;
import com.xckj.ibox.databinding.ActivityBrowserBinding;
import com.xckj.ibox.event.ShowTabBarEvent;
import com.xckj.ibox.framework.BaseActivity;
import com.xckj.ibox.global.Constant;
import com.xckj.ibox.view.CustomWebView;

import org.greenrobot.eventbus.EventBus;

/**
 * @Author: ChenYao
 * @Describe: 浏览器页面
 * @Date: 2023/6/19 19:06
 */
public class BrowserActivity extends BaseActivity<ActivityBrowserBinding> {
    private String h5UrlPath;

    public static void start(Context context, String urlPath) {
        context.startActivity(new Intent(context, BrowserActivity.class).putExtra(Constant.H5_URL_PATH, urlPath));
    }

    @Override
    protected boolean isStatusBarDarkFont() {
        return false;
    }

    @Override
    protected boolean isStatusBarEnabled() {
        return true;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_browser;
    }

    @Override
    protected void initData() {
        h5UrlPath = getIntent().getStringExtra(Constant.H5_URL_PATH);
        initWebView();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        String h5Url;
        if (h5UrlPath.startsWith(Constant.DOMAIN_PREFIX)) {
            h5Url = h5UrlPath;
        } else {
            h5Url = Constant.BASE_WEB_URL + h5UrlPath;
        }
        dataBinding.webView.setHorizontalScrollBarEnabled(false);
        dataBinding.webView.loadUrl(h5Url);
    }

    @Override
    protected void setListener() {

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onBackPressed() {
        EventBus.getDefault().post(new ShowTabBarEvent());
        if (dataBinding.webView.goBack()) {
            return;
        }
        super.onBackPressed();
    }
}
